package com.lv.suyiyong.hfr.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.databinding.LayoutHomeBottomTabNewYearBinding

class HomeBottomTabNewWidget @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : BaseHomeBottomTabNewWidget(context, attrs, defStyleAttr),
    View.OnClickListener {
    private var textViews: ArrayList<LinearLayout>? = null
    private var mIvHomeTab: ImageView? = null//声明成可空类型
    private var mIvCirTabNew: ImageView? = null//声明成可空类型
    private var mIvCartTabNew: ImageView? = null//声明成可空类型
    private var mIvMessageTabNew: ImageView? = null//声明成可空类型
    private var mIvMineTabNew: ImageView? = null//声明成可空类型
    private var mIvHome: ImageView? = null//声明成可空类型
    private var mIvFindGoods: ImageView? = null//声明成可空类型
    private var mIvCart: ImageView? = null//声明成可空类型
    private var mIvMessage: ImageView? = null//声明成可空类型
    private var mIvMine: ImageView? = null//声明成可空类型
    private var mTotalUnread: TextView? = null//声明成可空类型
    private var mTvHome: TextView? = null//声明成可空类型
    private var mTvFindGoods: TextView? = null//声明成可空类型
    private var mTvCart: TextView? = null//声明成可空类型
    private var mTvMessage: TextView? = null//声明成可空类型
    private var mTvMine: TextView? = null//声明成可空类型
    private var mCirTotal: TextView? = null//声明成可空类型
    private var mLlHomeATHome: LinearLayout? = null//声明成可空类型
    private var mLlBottomTabNew: LinearLayout? = null//声明成可空类型
    private var mViewLine: View? = null//声明成可空类型

    /**
     * 初始化 设置点击事件。
     *
     */
    init { //默认第一个碎片
        val view =
            LayoutHomeBottomTabNewYearBinding.inflate(LayoutInflater.from(context), this, true)
        view.apply {
            mIvHomeTab = ivHomeTabNew
            mIvCirTabNew = ivCirTabNew
            mIvCartTabNew = ivCartTabNew
            mIvMessageTabNew = ivMessageTabNew
            mIvMineTabNew = ivMineTabNew
            mTotalUnread = msgTotalUnreadNew
            mCirTotal = cirTotalUnreadNew
            mLlHomeATHome = llHomeATHomeNew
            mLlBottomTabNew = llBottomTabNew
            mIvHome = ivHome
            mIvFindGoods = ivFindGoods
            mIvCart = ivCart
            mIvMessage = ivMessage
            mIvMine = ivMine
            mTvHome = tvHome
            mTvFindGoods = tvFindGoods
            mTvCart = tvCart
            mTvMessage = tvMessage
            mTvMine = tvMine
            mViewLine = viewLine

            textViews = arrayListOf(
                llHomeATHomeNew,
                llHomeATCircleNew,
                llHomeATCartNew,
                llHomeATMessageNew,
                llHomeATMineNew
            )
        }

        for (textView in textViews!!) {
            textView.setOnClickListener(this)
        }
        mIvHomeTab?.setOnClickListener(this)
        mIvCirTabNew?.setOnClickListener(this)
        mIvCartTabNew?.setOnClickListener(this)
        mIvMessageTabNew?.setOnClickListener(this)
        mIvMine?.setOnClickListener(this)

    }

    /**
     * 销毁，避免内存泄漏
     */
    override fun destroy() {
        super.destroy()
        if (!textViews.isNullOrEmpty()) {
            textViews?.clear()
            textViews = null
        }
    }

    /**
     * 实现按钮的点击事件
     */
    override fun onClick(v: View) {
        when (v.id) {
            R.id.llHomeATHomeNew, R.id.iv_home_tab -> {
                otherClick()
                fragmentManger(0)
            }

            R.id.llHomeATCircleNew, R.id.iv_find_goods -> {
                cirClick()
                fragmentManger(1)
            }

            R.id.llHomeATCartNew, R.id.iv_cart -> {
                otherClick()
                fragmentManger(2)
            }

            R.id.llHomeATMessageNew, R.id.iv_message -> {
                otherClick()
                fragmentManger(3)
            }

            R.id.llHomeATMineNew, R.id.iv_mine -> {
                otherClick()
                fragmentManger(4)
            }
        }
    }

    private fun cirClick() {
        mViewLine?.visibility = View.GONE
        mLlBottomTabNew?.setBackgroundColor(Color.parseColor("#242424"))
        mTvHome?.setTextColor(Color.parseColor("#ffffff"))
        mTvCart?.setTextColor(Color.parseColor("#ffffff"))
        mTvMessage?.setTextColor(Color.parseColor("#ffffff"))
        mTvMine?.setTextColor(Color.parseColor("#ffffff"))
    }

    private fun otherClick() {
        mViewLine?.visibility = View.VISIBLE
        mLlBottomTabNew?.setBackgroundResource(com.lv.common.R.drawable.bg_ffeeee_fff)
        mTvHome?.setTextColor(resources.getColorStateList(com.lv.common.R.color.home_text_color_select_new))
        mTvCart?.setTextColor(resources.getColorStateList(com.lv.common.R.color.home_text_color_select_new))
        mTvMessage?.setTextColor(resources.getColorStateList(com.lv.common.R.color.home_text_color_select_new))
        mTvMine?.setTextColor(resources.getColorStateList(com.lv.common.R.color.home_text_color_select_new))
    }

    /**
     * fragment的切换 实现底部导航栏的切换
     *
     * @param position 序号
     */
    override fun fragmentManger(position: Int) {
        super.fragmentManger(position)
        for (j in textViews!!.indices) {
            textViews!![j].isSelected = position == j
        }
        when (position) {
            0 -> {
                mIvHomeTab?.visibility = VISIBLE
                mIvCirTabNew?.visibility = GONE
                mIvCartTabNew?.visibility = GONE
                mIvMessageTabNew?.visibility = GONE
                mIvMineTabNew?.visibility = GONE

                mIvHome?.visibility = View.INVISIBLE
                mIvFindGoods?.visibility = View.VISIBLE
                mIvCart?.visibility = View.VISIBLE
                mIvMessage?.visibility = View.VISIBLE
                mIvMine?.visibility = View.VISIBLE
            }

            1 -> {
//                mLlHomeATHome?.visibility = View.INVISIBLE
                mIvHomeTab?.visibility = GONE
                mIvCirTabNew?.visibility = VISIBLE
                mIvCartTabNew?.visibility = GONE
                mIvMessageTabNew?.visibility = GONE
                mIvMineTabNew?.visibility = GONE

                mIvHome?.visibility = View.VISIBLE
                mIvFindGoods?.visibility = View.INVISIBLE
                mIvCart?.visibility = View.VISIBLE
                mIvMessage?.visibility = View.VISIBLE
                mIvMine?.visibility = View.VISIBLE
            }

            2 -> {
                mIvHomeTab?.visibility = GONE
                mIvCirTabNew?.visibility = GONE
                mIvCartTabNew?.visibility = VISIBLE
                mIvMessageTabNew?.visibility = GONE
                mIvMineTabNew?.visibility = GONE

                mIvHome?.visibility = View.VISIBLE
                mIvFindGoods?.visibility = View.VISIBLE
                mIvCart?.visibility = View.INVISIBLE
                mIvMessage?.visibility = View.VISIBLE
                mIvMine?.visibility = View.VISIBLE
            }

            3 -> {
                mIvHomeTab?.visibility = GONE
                mIvCirTabNew?.visibility = GONE
                mIvCartTabNew?.visibility = GONE
                mIvMessageTabNew?.visibility = VISIBLE
                mIvMineTabNew?.visibility = GONE

                mIvHome?.visibility = View.VISIBLE
                mIvFindGoods?.visibility = View.VISIBLE
                mIvCart?.visibility = View.VISIBLE
                mIvMessage?.visibility = View.INVISIBLE
                mIvMine?.visibility = View.VISIBLE
            }

            4 -> {
                mIvHomeTab?.visibility = GONE
                mIvCirTabNew?.visibility = GONE
                mIvCartTabNew?.visibility = GONE
                mIvMessageTabNew?.visibility = GONE
                mIvMineTabNew?.visibility = VISIBLE

                mIvHome?.visibility = View.VISIBLE
                mIvFindGoods?.visibility = View.VISIBLE
                mIvCart?.visibility = View.VISIBLE
                mIvMessage?.visibility = View.VISIBLE
                mIvMine?.visibility = View.INVISIBLE
            }

        }
//        if (position == 0) {
//            mIvHomeTab?.visibility = VISIBLE
//            mLlHomeATHome?.visibility=View.INVISIBLE
//        } else {
//            mIvHomeTab?.visibility = GONE
//            mLlHomeATHome?.visibility=View.VISIBLE
//        }
        listener?.OnClickTab(position)
    }

    fun setMessageCount(count: String) {
        if (count == "0") {
            mTotalUnread?.visibility = GONE
        } else {
            mTotalUnread?.visibility = VISIBLE
        }
        mTotalUnread?.text = count
    }

    fun setCartCount(count: String) {
        if (count == "0") {
            mCirTotal?.visibility = GONE
        } else {
            mCirTotal?.visibility = VISIBLE
        }
        mCirTotal?.text = count
    }

    fun setTabStyle(Tab: Int) {
        if (Tab == 1) {
            cirClick()
        } else {
            otherClick()
        }
    }

    interface TabPagerListener {
        fun OnClickTab(position: Int)
    }

    fun setOnTabPagerListener(l: TabPagerListener) {
        listener = l
    }


    var listener: TabPagerListener? = null

}